#include "TratamentoMemoria.h"
#include <i2c.h>
#include "ConfigApp.h"
#include "WirelessProtocols/Console.h"
#include "Transceivers/Transceivers.h"
#include "WirelessProtocols/SymbolTime.h"
#include "Transceivers/Security.h"
#include "WirelessProtocols/MCHP_API.h"
#include "TratamentoRTC.h"

BOOL gravarDado(int *aux) {
    if (!getSemaforoI2C()) {
        TMR8 = 0;
        PR8 = 40000;
        IFS3bits.T8IF = 0;
        T8CONbits.TON = 1;
        int pagina = 0;
        int posMem = 0;
        setSemaforoI2C(TRUE);
        int config2 = 0x53;
        /* Configure I2C for 7 bit address mode  */
        int config1 = (I2C1_ON & I2C1_IDLE_CON & I2C1_CLK_HLD
                & I2C1_IPMI_DIS & I2C1_7BIT_ADD
                & I2C1_SLW_DIS & I2C1_SM_DIS &
                I2C1_GCALL_DIS & I2C1_STR_DIS &
                I2C1_ACK & I2C1_ACK_DIS & I2C1_RCV_DIS &
                I2C1_STOP_DIS & I2C1_RESTART_DIS
                & I2C1_START_DIS);
        OpenI2C1(config1, config2);
        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        MasterWriteI2C1(0xA0);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        MasterWriteI2C1(0x00);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        MasterWriteI2C1(0xA1);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        pagina = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        AckI2C1();
        while (I2C1CONbits.ACKEN);
        posMem = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        while (I2C1STATbits.ACKSTAT);
        NotAckI2C1();
        while (I2C1CONbits.ACKEN);
        IdleI2C1();
        StopI2C1();
        while (I2C1CONbits.PEN);


        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        /*
                if ((pagina == 7) && ((255 - posMem)<(alicatesporNo + 8))) {
                    pagina = 0;
                    posMem = 0;
                                LED_1=!LED_1;
                    //Arruma ponteiro
                }
         */
        //StopI2C
        //FechaI2C
        //mandarComandosGravaAlicates()-> aqui dentro verifica ->while(RX==1);;
        //->Abre I2C
        //Starta
        IdleI2C1();
        MasterWriteI2C1((0xA0) | (pagina << 1));
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        MasterWriteI2C1(posMem);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        int countTempFake;
        for (countTempFake = 0; countTempFake < alicatesporNo; countTempFake++) {
            IdleI2C1();
            MasterWriteI2C1(*aux);
            while (I2C1STATbits.TBF);
            while (I2C1STATbits.ACKSTAT);
            if (posMem == 0xff && pagina == 7) {
                posMem = 0x02;
                pagina = 0;
            } else {

                if (posMem == 0xff) {
                    pagina++;
                    posMem++;
                } else {
                    posMem++;
                }
            }
        }
        int i;
        for (i = 0; i < 7; i++) {
            IdleI2C1();
            MasterWriteI2C1(*aux);
            while (I2C1STATbits.TBF);
            while (I2C1STATbits.ACKSTAT);
            if (posMem == 0xff && pagina == 7) {
                posMem = 0x02;
                pagina = 0;
            } else {

                if (posMem == 0xff) {
                    pagina++;
                    posMem++;
                } else {
                    posMem++;
                }
            }
            aux++;
        }
        IdleI2C1();
        MasterWriteI2C1(0xFF);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        if (posMem == 0xff && pagina == 7) {
            posMem = 0x02;
            pagina = 0;
        } else {
            if (posMem == 0xff) {
                pagina++;
                posMem++;
            } else {
                posMem++;
            }
        }
        IdleI2C1();
        StopI2C1();
        while (I2C1CONbits.PEN);

        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        IdleI2C1();
        MasterWriteI2C1(0xA0);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        MasterWriteI2C1(0x00);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        MasterWriteI2C1(pagina);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        MasterWriteI2C1(posMem);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        StopI2C1();
        while (I2C1CONbits.PEN);
        CloseI2C1();
        T8CONbits.TON = 0;
        setSemaforoI2C(FALSE);
        return TRUE;
    } else {
        return FALSE;
    }
}

BOOL lerDado(int *aux) {
    if (!getSemaforoI2C()) {
        setSemaforoI2C(TRUE);
        TMR8 = 0;
        PR8 = 40000;
        IFS3bits.T8IF = 0;
        T8CONbits.TON = 1;
        int config2 = 0x53;
        int vetorMemoria[2046];
        int config1 = (I2C1_ON & I2C1_IDLE_CON & I2C1_CLK_HLD
                & I2C1_IPMI_DIS & I2C1_7BIT_ADD
                & I2C1_SLW_DIS & I2C1_SM_DIS &
                I2C1_GCALL_DIS & I2C1_STR_DIS &
                I2C1_ACK & I2C1_ACK_DIS & I2C1_RCV_DIS &
                I2C1_STOP_DIS & I2C1_RESTART_DIS
                & I2C1_START_DIS);
        OpenI2C1(config1, config2);
        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        MasterWriteI2C1(0xA0);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        MasterWriteI2C1(0x02);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        StartI2C1();
        while (I2C1CONbits.SEN);
        MasterWriteI2C1(0xA1);
        while (I2C1STATbits.TBF);
        while (I2C1STATbits.ACKSTAT);
        int j;
        for (j = 0; j < 2045; j++) {
            vetorMemoria[j] = MasterReadI2C1();
            while (I2C1STATbits.RBF);
            while (I2C1STATbits.ACKSTAT);
            IdleI2C1();
            AckI2C1();
            while (I2C1CONbits.ACKEN);
        }
        vetorMemoria[2045] = MasterReadI2C1();
        while (I2C1STATbits.RBF);
        while (I2C1STATbits.ACKSTAT);
        IdleI2C1();
        NotAckI2C1();
        while (I2C1CONbits.ACKEN);
        IdleI2C1();
        StopI2C1();
        while (I2C1CONbits.PEN);
        CloseI2C1();
        T8CONbits.TON = 0;
        BOOL terminouVetor = FALSE;
        BOOL achouHorario = FALSE;
        int temp;
        int vetorHorario[7];
        for (temp = 0; temp < 7; temp++) {
            vetorHorario[temp] = *aux;
            aux++;
        }
        int count = 0;
        while (!(terminouVetor | achouHorario)) {
            if ((vetorHorario[0] == vetorMemoria[count]) && (vetorHorario[1] == vetorMemoria[count + 1]) && (vetorHorario[2] == vetorMemoria[count + 2]) && (vetorHorario[3] == vetorMemoria[count + 3]) && (vetorHorario[4] == vetorMemoria[count + 4]) && (vetorHorario[5] == vetorMemoria[count + 5]) && (vetorHorario[6] == vetorMemoria[count + 6]) && (vetorMemoria[count + 7] == 0xFF)) {
                achouHorario = TRUE;
            } else {
                count++;
            }
            if (count == 2038) {
                terminouVetor = TRUE;
            }
        }
        if (achouHorario) {
            int countTemp;
            int countAux = 1;
            for (countTemp = 0; countTemp < alicatesporNo; countTemp++) {
                MiApp_WriteData(vetorMemoria[count - countAux]);
                countAux++;
            }
        }
        setSemaforoI2C(FALSE);
        if (achouHorario) {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {
        return FALSE;
    }
}
